// ==UserScript==
// @name         51110
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://www.51110.com/loginsuccess
// @grant        none
// @require http://www.51110.com/js/jquery.js
// @run-at document-idle
// ==/UserScript==

var data = [{
	cameraId:"",
	cameraState:"1",
    isexpire:"0",
    cameraName:"中一班",
    startTime:"08:00",
    endTime:"17:00",
    openTimesList:"时间",
    flag:true
}];

var cameraInit = false;

(function() {
    'use strict';

    // Your code here...
    //备份jquery的ajax方法
    var _ajax=$.ajax;

    //重写jquery的ajax方法
    $.ajax=function(opt){
        //备份opt中error和success方法
        var fn = {
            error:function(xhr, textStatus, errorThrown){},
            success:function(data, textStatus){}
        };
        if(opt.error){
            fn.error=opt.error;
        }
        if(opt.success){
            fn.success=opt.success;
        }

        var showLoading = false;
        var loadingText = '页面加载中....';
        if(opt.showLoading){
            showLoading = true;
            if(opt.loadingText){
                loadingText = opt.loadingText;
            }
        }

        var exceptionStr = "org.civframework.core.common.exception.BusinessException: ";

        //扩展增强处理
        var _opt = $.extend(opt,{
            error:function(xhr, textStatus, errorThrown){
                //错误方法增强处理
                try{
                    var html = $.parseHTML(xhr.responseText);
                    if(-1 != $(html[0]).text().indexOf("Apache Tomcat")){//struts.devMode=false
                        var stack = $(html[9]).text().split(/\r?\n/);
                        if(0 == stack[0].indexOf(exceptionStr)){
                            //alert(stack[0].substr(exceptionStr.length));
                        }
                    }
                }catch(e){

                }
                fn.error(xhr, textStatus, errorThrown);
            },
            success:function(adata, textStatus){
                debugger;
                if(!cameraInit&&$(this)[0].url=="/zhifubao/babyPrompt.do"){
                    cameraInit = true;
                    $(".expired").hide();
                    $(".video-area").show();
                    $("#mid").html("jz009000");
                    for(var i=0;i<data.length;i++){
                        var newDom = $("#lim").clone();
                        var cameraStateName="";
                        if(data[i].cameraState=="1" && data[i].isexpire=="0"){
                            cameraStateName="在线";
                            newDom.find(".img-box").show();
                            newDom.find(".img-boxs").hide();
                            newDom.find(".img-boxss").hide();
                        }else if(data[i].cameraState=="0" && data[i].isexpire=="0"){
                            cameraStateName="离线"; 
                            newDom.find(".img-box").hide();
                            newDom.find(".img-boxs").show();
                            newDom.find(".img-boxss").hide();
                        }else if(data[i].isexpire=="1"){
                            cameraStateName="已到期"; 
                            newDom.find(".img-box").hide();
                            newDom.find(".img-boxs").hide();
                            newDom.find(".img-boxss").show();
                        }

                        // newDom.find(".cameraStateName").html(cameraStateName); 
                        newDom.find(".class").html(data[i].cameraName);
                        if(data[i].flag){
                            newDom.find(".time").html("正在直播 "+data[i].startTime+"-"+data[i].endTime);
                        }else{
                            newDom.find(".time").html("即将开放 "+data[i].startTime+"-"+data[i].endTime);
                        }

                        newDom.find(".cameraId").html(data[i].cameraId); 

                        newDom.find(".openTimesList").data("openTimesLists",data[i].openTimesList); 
                        $(".video-menu").append(newDom);
                        newDom.show();
                    }
                }else if(cameraInit&&$(this)[0].url=="/parent/playVideo.do"){
                    
                }
                //
                fn.success(adata, textStatus);
            },
            beforeSend:function(xhr){
                //提交前回调方法
                if(showLoading){
                    //easyui
                    $.messager.progress({
                        text : loadingText,
                        interval : 100
                    });
                }
            },
            complete:function(xhr, textStatus){
                //请求完成后回调函数 (请求成功或失败之后均调用)。
                if(showLoading){
                    //easyui
                    $.messager.progress('close');
                }
            }
        });
        _ajax(_opt);
    };
})();